#ifndef UNITTEST_EXECUTE_TEST_HPP
#define UNITTEST_EXECUTE_TEST_HPP

#include "TestDetails.hpp"
#include "TestResults.hpp"
#include "ExceptionMacros.hpp"
#include "SignalTranslator.hpp"
#include "RequiredCheckException.hpp"
#include "AssertException.hpp"
#include "CurrentTest.hpp"
#include "MemoryOutStream.hpp"

namespace UnitTest {
    template< typename T >
    void ExecuteTest(T& testObject, TestDetails const& details, bool isMockTest)
    {
        if (isMockTest == false)
            CurrentTest::Details() = &details;

        UNITTEST_IMPL_TRY
            ({
                UNITTEST_THROW_SIGNALS_POSIX_ONLY
                testObject.RunImpl();
        })
        UNITTEST_IMPL_CATCH(RequiredCheckException, e, { (void)e; })
        UNITTEST_IMPL_CATCH(AssertException, e, { (void)e; })
        UNITTEST_IMPL_CATCH(std::exception, e,
        {
            MemoryOutStream stream;
            stream << "Unhandled exception: " << e.what();
            CurrentTest::Results()->OnTestFailure(details, stream.GetText());
        })
        UNITTEST_IMPL_CATCH_ALL
            ({
                CurrentTest::Results()->OnTestFailure(details, "Unhandled exception: test crashed");
      })
    }
}

#endif /* UNITTEST_EXECUTE_TEST_HPP */
